//
//  YWMoreAnchorViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWMoreAnchorViewController.h"
#import "YWTheHotestModel.h"
#import "YWTheHotestAnchorTableViewCell.h"
#import "YWAnchorIntroduceViewController.h"

@interface YWMoreAnchorViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UISegmentedControl *seg;

@property (nonatomic, copy) NSString *condition;

@property (nonatomic, strong) UITableView *anchorTableView;

@property (nonatomic,strong) NSMutableArray *theHotestAnchorArray;

@end

@implementation YWMoreAnchorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = _titleString;
    [self getView];
    _condition = @"hot";
    self.theHotestAnchorArray = [NSMutableArray array];
    [self getDataByCategory_name:_nameString Condition:_condition];
}

- (void)getView
{
    [self getSegView];
    [self getAnchorTableView];
}

#pragma mark ------ 分页控制器 ------
- (void)getSegView
{
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"最火", @"最新"]];
    _seg.frame = CGRectMake(10, 64, SCREENWIDTH - 20, 30);
    _seg.selectedSegmentIndex = 0;
    [_seg addTarget:self action:@selector(didClickedSegmentControl:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_seg];
}

- (void)didClickedSegmentControl:(UISegmentedControl *)seg
{
    switch (seg.selectedSegmentIndex) {
        case 0:
            _condition = @"hot";
            [_theHotestAnchorArray removeAllObjects];
            [self getDataByCategory_name:_nameString Condition:_condition];
            break;
        case 1:
            _condition = @"new";
            [_theHotestAnchorArray removeAllObjects];
            [self getDataByCategory_name:_nameString Condition:_condition];
            break;
        default:
            break;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

#pragma mark ------ 表视图 ------
- (void)getAnchorTableView
{
    self.anchorTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 94, SCREENWIDTH, SCREENHEIGHT - 30 - 64) style:UITableViewStylePlain];
    _anchorTableView.delegate = self;
    _anchorTableView.dataSource = self;
    [self.view addSubview:_anchorTableView];
    
    [_anchorTableView registerClass:[YWTheHotestAnchorTableViewCell class] forCellReuseIdentifier:@"theHotestCell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _theHotestAnchorArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150 * FITHEIGHT;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWTheHotestModel *model = [_theHotestAnchorArray objectAtIndex:indexPath.row];
    YWTheHotestAnchorTableViewCell *hotestCell = [tableView dequeueReusableCellWithIdentifier:@"theHotestCell"];
    hotestCell.hotestModel = model;
    return hotestCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWTheHotestModel *model = [_theHotestAnchorArray objectAtIndex:indexPath.row];
    YWAnchorIntroduceViewController *aivc = [[YWAnchorIntroduceViewController alloc] init];
    aivc.hidesBottomBarWhenPushed = YES;
    aivc.uid = model.uid;
    [self.navigationController pushViewController:aivc animated:YES];
}

- (void)getDataByCategory_name:(NSString *)name
                     Condition:(NSString *)condition
{
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_list?category_name=%@&condition=%@&device=android&page=1&per_page=20&statEvent=pageview%%2Fuserlist", name, condition];
    NSLog(@"%@", urlString);
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:LTJSON Success:^(id result) {
        NSArray *tempArray = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArray) {
            YWTheHotestModel *model = [[YWTheHotestModel alloc] initWithDic:dic];
            [_theHotestAnchorArray addObject:model];
        }
        [_anchorTableView reloadData];
    } Failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
